package com.gm.commonlib.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;



import java.io.File;
import java.util.List;

import androidx.core.content.FileProvider;

import com.gm.commonlib.views.toast.ToastUtil;


public class AndroidUtil {

    /**
     * AndroidManifest.xml
     * <android.support.v4.content.FileProvider
     * ......
     */
    private static String FILE_AUTHORITIES = ".fileprovider";

    public static String getApplicationId(Context context) {
        if (context != null) {
            return context.getApplicationContext().getPackageName();
        }
        return "";
    }

    public static void setLightValue(Activity activity, float lightValue) {
        if (activity == null) {
            return;
        }
        Window window = activity.getWindow();
        if (window == null) {
            return;
        }
        WindowManager.LayoutParams lp = window.getAttributes();
        if (lp != null) {
            lp.screenBrightness = lightValue * (1f / 255f);
            window.setAttributes(lp);
        }
    }

    public static float getLightValue(Activity activity) {
        if (activity != null) {
            Window window = activity.getWindow();
            if (window != null) {
                WindowManager.LayoutParams lp = window.getAttributes();
                if (lp != null) {
                    return lp.screenBrightness;
                }
            }
        }
        return 0f;
    }

    public static void registerReceiver(Activity activity, BroadcastReceiver broadcastReceiver, IntentFilter filter) {
        if (activity == null || broadcastReceiver == null || filter == null) {
            return;
        }
        activity.registerReceiver(broadcastReceiver, filter);
    }

    public static void unregisterReceiver(Activity activity, BroadcastReceiver broadcastReceiver) {
        if (activity == null || broadcastReceiver == null) {
            return;
        }
        activity.unregisterReceiver(broadcastReceiver);
    }



    public static void acquireWakeLock(PowerManager.WakeLock wakeLock) {
        if (wakeLock != null) {
            wakeLock.acquire();
        }
    }

    public static void releaseWakeLock(PowerManager.WakeLock wakeLock) {
        if (wakeLock != null) {
            wakeLock.release();
        }
    }

    public static void keepScreenOn(Activity activity) {
        if (activity != null) {
            Window window = activity.getWindow();
            if (window != null) {
                window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
        }
    }





    public static void sendBroadcast(Context context, String action) {
        if (context != null && !TextUtils.isEmpty(action)) {
            context.sendBroadcast(new Intent(action));
        }
    }



    /**
     * 发送短信
     *
     * @param phoneNumber 接收短信的号码
     * @param context
     */
    public static void sendMsg(String phoneNumber, Context context) {
        Uri uri = Uri.parse("smsto:" + phoneNumber);
        Intent intentMessage = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intentMessage);
    }

    public static ClipboardManager getClipboardManager(Context context) {
        return (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    }

    public static void copy(Context context, String data) {
        clipboardCopy(getClipboardManager(context), data);
    }

    public static void clipboardCopy(ClipboardManager clipboard, String data) {
        if (clipboard != null) {
            try {
                //创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
                ClipData clipData = ClipData.newPlainText(null, data);
                //把数据集设置（复制）到剪贴板
                clipboard.setPrimaryClip(clipData);
            } catch (Throwable throwable) {
            }
        }
    }

    public static CharSequence clipboardContent(Context context) {
        return clipboardContent(getClipboardManager(context));
    }

    public static CharSequence clipboardContent(ClipboardManager clipboard) {
        if (clipboard != null) {
            //获取剪贴板的剪贴数据集
            ClipData clipData = clipboard.getPrimaryClip();
            try {
                if (clipData != null && clipData.getItemCount() > 0) {
                    //从数据集中获取（已复制）第一条文本数据
                    return clipData.getItemAt(0).getText();
                }
            } catch (Throwable throwable) {
            }
        }
        return "";
    }

    public static void setClipChangedListener(Context context, final ClipboardManager.OnPrimaryClipChangedListener l) {
        setClipChangedListener(getClipboardManager(context), l);
    }

    public static void setClipChangedListener(ClipboardManager clipboard, final ClipboardManager.OnPrimaryClipChangedListener l) {
        if (clipboard != null) {
            clipboard.addPrimaryClipChangedListener(l);
        }
    }

    public static void removeClipChangedListener(Context context, final ClipboardManager.OnPrimaryClipChangedListener l) {
        removeClipChangedListener(getClipboardManager(context), l);
    }

    public static void removeClipChangedListener(ClipboardManager clipboard, final ClipboardManager.OnPrimaryClipChangedListener l) {
        if (clipboard != null) {
            clipboard.removePrimaryClipChangedListener(l);
        }
    }


    public static void showHtml(Context context, String url) {
        try {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        } catch (Exception e) {
           ToastUtil.showToast("地址错误打开失败");
        }

    }

    public static boolean isSelfAppInTop(Context context) {
        try {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            if (am == null) {
                return false;
            }
            List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(2);

            if (list.isEmpty()) {
                return false;
            }

            return false;
        } catch (Throwable throwable) {
            return false;
        }
    }



    public static void openGaoDeMap( double slat, double slon,double dlat, double dlon,Activity activity) {
       // if (checkMapAppsIsExist(this, "com.autonavi.minimap")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setPackage("com.autonavi.minimap");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse("amapuri://route/plan/?sid=&slat="+slat+
                    "&slon="+slon+"&sname=&did=&dlat="+dlat+"&dlon="+dlon+"&dname=B&dev=0&t=0"));
            activity.startActivity(intent);
       // } else {

       // }
    }
}
